- Plotly is a very powerful library for making interactive plots. Its a little weird in that plotly is developed by a independent company, but makes their library open source. If you use plotly, they do add a small watermark to your plots
-Most of plotly is actually a javascript library, and the functions in R are wrapper on top of the java script code.
- but its worth it, because you can create some pretty cool plots
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)
fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E'))
## This version of Shiny is designed to work with 'htmlwidgets' >= 1.5.
## Please upgrade via install.packages('htmlwidgets').
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
yaxis = list(title = 'Gross horsepower'),
zaxis = list(title = '1/4 mile time')))
fig
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
- plotly has a ggplot-esque API (thank you yigit Erol for the example)
library(plotly)
x <- c(2015:2020)
Beverages <- rnorm(x, mean = 10)
Grocery <- rnorm(x, mean = 1)
Snack <- rnorm(x, mean = 5)
x <- c(2015:2020)
df <- data.frame(x, Beverages, Grocery, Snack)
df
## x Beverages Grocery Snack
## 1 2015 9.300848 0.863743461 4.356702
## 2 2016 10.288228 -0.008585506 3.601982
## 3 2017 8.466895 0.536291019 3.710398
## 4 2018 10.046593 1.823362151 4.698964
## 5 2019 9.312002 0.343240480 6.257437
## 6 2020 10.103984 0.134480095 5.655798
- now the actual plot.
plot_ly is very loose with how it populates data, so what ever parameters we don’t specify, it will try and guess from the data we provide
fig <- plot_ly(data = df, x = ~x, marker=list(size=10))
fig
## No trace type specified:
## Based on info supplied, a 'histogram' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#histogram
- we didn’t provide a mapping what type of plot we wanted, and so it defaults to a bar graph
plot_ly(data = df, x = ~x,type = 'scatter', marker=list(size=10))
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
-we told it scatter plot, but didn’t provide any Y data
plot_ly(data = df, x = ~x,tmarker=list(size=10)) %>%
add_trace(y = ~Beverages, name = 'Beverage',mode = 'lines') %>%
add_trace(y = ~Grocery, name = 'Grocery', mode = 'lines+markers') %>%
add_trace(y = ~Snack, name = 'Snack', mode = 'markers') %>%
layout(title = 'Sales by Catergories Between 2015 & 2020',
yaxis = list(title = 'Average Sales'),
xaxis = list(title = 'Time') )
- note that we build graphs iteratively, unlike ggplot where all plots of a certain type are specified at once
- plotly does provide function to auto convert
ggplots into plotly plots
df_long <- df %>% pivot_longer(-x)
gg <- ggplot(df_long) +
geom_line(aes(x=x, y=value, color = name))
gg

gg %>% ggplotly()
- This only works for simple plots; the 3D plot above couldn’t be created like this, because ggplot only understands x and y aesthetics
- Between these two libraries, you should have enough to start making your own plots